package com.feihua.starter.web.modules.hdb.mvc;

import com.feihua.framework.rest.ResponseJsonRender;
import com.feihua.framework.rest.interceptor.RepeatFormValidator;
import com.feihua.framework.rest.modules.common.mvc.BaseController;
import com.feihua.starter.service.api.ApiHdbUserExtendPoService;
import com.feihua.starter.service.modules.hdb.dto.HdbUserExtendDto;
import com.feihua.starter.service.modules.hdb.dto.SearchHdbUserExtendsConditionDto;
import com.feihua.starter.service.modules.hdb.po.HdbUserExtendPo;
import com.feihua.utils.http.httpServletResponse.ResponseCode;
import feihua.jdbc.api.pojo.PageAndOrderbyParamDto;
import feihua.jdbc.api.pojo.PageResultDto;
import feihua.jdbc.api.utils.OrderbyUtils;
import feihua.jdbc.api.utils.PageUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 活动吧用户管理
 * Created by revolver
 */
@RestController
@RequestMapping("/hdb")
public class HdbUserExtendController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(HdbUserExtendController.class);

    @Autowired
    private ApiHdbUserExtendPoService apiHdbUserExtendPoService;

    /**
     * 单资源，更新
     *
     * @param hdbUserExtendDto
     *
     * @return
     */
    @RepeatFormValidator
    @RequiresPermissions("hdb:userextend:update")
    @RequestMapping(value = "/userextend", method = RequestMethod.PUT)
    public ResponseEntity update(@RequestBody HdbUserExtendDto hdbUserExtendDto) {
        logger.info("更新活动吧用户开始");
        logger.info("当前登录用户id:{}", getLoginUser().getId());
        logger.info("活动吧用户id:{}", hdbUserExtendDto.getId());
        ResponseJsonRender resultData = new ResponseJsonRender();
        HdbUserExtendPo hdbUserExtendPo = new HdbUserExtendPo();
        BeanUtils.copyProperties(hdbUserExtendDto, hdbUserExtendPo);
        //
        apiHdbUserExtendPoService.preUpdate(hdbUserExtendPo, getLoginUser().getId());
        int r = apiHdbUserExtendPoService.updateByPrimaryKeySelective(hdbUserExtendPo);
        if (r <= 0) {
            // 更新失败，资源不存在
            resultData.setCode(ResponseCode.E404_100001.getCode());
            resultData.setMsg(ResponseCode.E404_100001.getMsg());
            logger.info("code:{},msg:{}", resultData.getCode(), resultData.getMsg());
            logger.info("更新活动吧用户结束，失败");
            return new ResponseEntity(resultData, HttpStatus.NOT_FOUND);
        } else {
            // 更新成功，已被成功创建
            logger.info("更新的活动吧用户id:{}", hdbUserExtendDto.getId());
            logger.info("更新活动吧用户结束，成功");
            return new ResponseEntity(resultData, HttpStatus.CREATED);
        }
    }
    /**
     * 单资源，设置管理员
     *
     * @param hdbUserExtendDto
     *
     * @return
     */
    @RepeatFormValidator
    @RequiresPermissions("hdb:userextend:setmanager")
    @RequestMapping(value = "/userextend/setmanager", method = RequestMethod.PUT)
    public ResponseEntity setManager(@RequestBody HdbUserExtendDto hdbUserExtendDto) {
        logger.info("更新活动吧设置管理员用户开始");
        logger.info("当前登录用户id:{}", getLoginUser().getId());
        logger.info("活动吧用户id:{}", hdbUserExtendDto.getId());
        ResponseJsonRender resultData = new ResponseJsonRender();
        HdbUserExtendPo hdbUserExtendPo = new HdbUserExtendPo();
        hdbUserExtendPo.setType(hdbUserExtendDto.getType());
        hdbUserExtendPo.setId(hdbUserExtendDto.getId());
        //
        apiHdbUserExtendPoService.preUpdate(hdbUserExtendPo, getLoginUser().getId());
        int r = apiHdbUserExtendPoService.updateByPrimaryKeySelective(hdbUserExtendPo);
        if (r <= 0) {
            // 更新失败，资源不存在
            resultData.setCode(ResponseCode.E404_100001.getCode());
            resultData.setMsg(ResponseCode.E404_100001.getMsg());
            logger.info("code:{},msg:{}", resultData.getCode(), resultData.getMsg());
            logger.info("更新活动吧设置管理员用户结束，失败");
            return new ResponseEntity(resultData, HttpStatus.NOT_FOUND);
        } else {
            // 更新成功，已被成功创建
            logger.info("更新的活动吧用户id:{}", hdbUserExtendDto.getId());
            logger.info("更新活动吧设置管理员用户结束，成功");
            return new ResponseEntity(resultData, HttpStatus.CREATED);
        }
    }

    /**
     * 单资源，获取id活动吧用户
     *
     * @param id
     *
     * @return
     */
    @RepeatFormValidator
    @RequiresPermissions("hdb:userextend:getById")
    @RequestMapping(value = "/userextend/id/{id}", method = RequestMethod.GET)
    public ResponseEntity getById(@PathVariable String id) {

        ResponseJsonRender resultData = new ResponseJsonRender();
        HdbUserExtendDto baseDataScopeDto = apiHdbUserExtendPoService.selectByPrimaryKey(id, false);
        if (baseDataScopeDto != null) {
            resultData.setData(baseDataScopeDto);
            return new ResponseEntity(resultData, HttpStatus.OK);
        } else {
            resultData.setCode(ResponseCode.E404_100001.getCode());
            resultData.setMsg(ResponseCode.E404_100001.getMsg());
            return new ResponseEntity(resultData, HttpStatus.NOT_FOUND);
        }
    }

    /**
     * 单资源，获取当前登录活动吧用户
     *
     * @param
     *
     * @return
     */
    @RepeatFormValidator
    @RequiresPermissions("hdb:userextend:getByCurrUserId")
    @RequestMapping(value = "/userextend", method = RequestMethod.GET)
    public ResponseEntity getByCurrUserId() {

        ResponseJsonRender resultData = new ResponseJsonRender();
        HdbUserExtendDto baseDataScopeDto = apiHdbUserExtendPoService.selectByUserId(getLoginUserId());
        if (baseDataScopeDto != null) {
            resultData.setData(baseDataScopeDto);
            return new ResponseEntity(resultData, HttpStatus.OK);
        } else {
            resultData.setCode(ResponseCode.E404_100001.getCode());
            resultData.setMsg(ResponseCode.E404_100001.getMsg());
            return new ResponseEntity(resultData, HttpStatus.NOT_FOUND);
        }
    }

    /**
     * 复数资源，搜索活动吧用户
     *
     * @param dto
     *
     * @return
     */
    @RepeatFormValidator
    @RequiresPermissions("hdb:userextend:search")
    @RequestMapping(value = "/userextends", method = RequestMethod.GET)
    public ResponseEntity search(SearchHdbUserExtendsConditionDto dto) {

        ResponseJsonRender resultData = new ResponseJsonRender();
        PageAndOrderbyParamDto pageAndOrderbyParamDto = new PageAndOrderbyParamDto(PageUtils.getPageFromThreadLocal(), OrderbyUtils.getOrderbyFromThreadLocal());
        // 设置当前登录用户id
        PageResultDto<HdbUserExtendDto> list = apiHdbUserExtendPoService.searchHdbUserExtendsDsf(dto, pageAndOrderbyParamDto);

        if (CollectionUtils.isNotEmpty(list.getData())) {
            resultData.setData(list.getData());
            resultData.setPage(list.getPage());
            return new ResponseEntity(resultData, HttpStatus.OK);
        } else {
            resultData.setCode(ResponseCode.E404_100001.getCode());
            resultData.setMsg(ResponseCode.E404_100001.getMsg());
            return new ResponseEntity(resultData, HttpStatus.NOT_FOUND);
        }
    }
}
